package ua.com.ulitko.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
import ua.com.ulitko.client.entity.Album;
import ua.com.ulitko.client.entity.Performer;
import ua.com.ulitko.client.entity.Song;
import ua.com.ulitko.client.service.PerformerService;
import ua.com.ulitko.client.service.PerformerServiceAsync;

import java.util.ArrayList;

/**
 * @author suamo ( 01.04.12 2:30 )
 */
public class AddSongPanel extends VerticalPanel {
    private TextBox performerBox = new TextBox();
    private TextBox albumBox = new TextBox();
    private TextBox songBox = new TextBox();
    private TextArea textBox = new TextArea();


    public AddSongPanel() {
        add(getRow(new Label("performer:"), performerBox));
        add(getRow(new Label("album:"), albumBox));
        add(getRow(new Label("song name:"), songBox));
        add(getRow(new Label("song text:"), textBox));

        Button button = new Button("Save");
        button.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                PerformerServiceAsync serv = PerformerService.Inst.getInstance();
//                ServiceDefTarget endpoint = (ServiceDefTarget) serv;
//                endpoint.setServiceEntryPoint(GWT.getModuleBaseURL() + "PerformerService");
                ArrayList<Song> songList = new ArrayList<Song>();
                songList.add(new Song(songBox.getText(), textBox.getText()));

                Album album = new Album(albumBox.getText(), songList);
                ArrayList<Album> albumList = new ArrayList<Album>();

                albumList.add(album);
                serv.addPerformer(new Performer(performerBox.getText(), albumList), new AsyncCallback<Void>() {
                    @Override
                    public void onFailure(Throwable caught) {
                        caught.printStackTrace();
                    }

                    @Override
                    public void onSuccess(Void result) {

                    }
                });
            }
        });
        add(button);
    }

    private HorizontalPanel getRow(Label performer, Widget box) {
        HorizontalPanel panel = new HorizontalPanel();
        panel.add(performer);
        panel.add(box);
        return panel;
    }
}
